<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2017/3/10
 * Time: 15:04
 */

namespace Information\Controller;


use Common\Controller\AdminbaseController;
use Information\Model\InformationModel;

class InformationController extends AdminbaseController
{
    private $information_model;

    public function __construct()
    {
        parent::__construct();
        $this->information_model = new InformationModel();
    }

    public function lists() {
        //搜索条件
        $where = array();
        if(IS_POST) {
            $title = I('post.title');
            if($title) {
                $where['title'] = array('like', '%'.$title.'%');
            }

        } else {
            $title = I('get.title');
            if($title) {
                $where['title'] = array('like', '%'.$title.'%');
            }

        }


        $count = $this->information_model
            ->where($where)
            ->count();
        $page = $this->page($count, C('PAGE_NUMBER'));
        $infomations = $this->information_model
            ->where($where)
            ->field('title, create_time, id, cover')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order('id desc')
            ->select();

        foreach($infomations as $k => $v) {
            $result[$k]['str_manage'] .= '<a class="js-ajax-btn-dialog" href="' . U('Information/edit', ['id' => $v['id']]) . '">编辑</a> | <a class="js-ajax-btn-dialog js-ajax-delete" href="' . U('Information/delete', ['id' => $v['id']]) . '">删除</a>';
            $lists .= '<tr>
               <td>' . ($k + 1) . '</td>
               <td>' . $v['id'] . '</td>
               <td>' . '<a class="js-ajax-btn-dialog" href="' . U('Information/detail', ['id' => $v['id']]) . '">'.$v['title'].'</a>' . '</td>
               <td><img src="' . setUrl($v['cover']) . '?imageView2/2/h/30"></td>
               <td>'.$v['create_time'] . '</td>
               <td>' . $result[$k]['str_manage'] . '</td>
           </tr>';

        }

        $this->assign('options', $options);
        $this->assign('lists', $lists);
        $this->assign('formget', I(''));
        $this->assign('Page',$page->show());
        $this->display();
    }

    /**
     * 添加
     */
    public function add() {
        $types = $this->information_model->types;
        foreach($types as $k => $v) {
            $options .= '<option value="'.$v['id'].'" >'.$v['name'].'</option>';
        }
        if(IS_POST) {
            $image = upload_img('Information');
            if(!$image) {
                $this->error('封面不能为空');
            }
            /*if (!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])) {
                foreach ($_POST['photos_url'] as $key => $url) {
                    $photourl = sp_asset_relative_url($url);
                    $_POST['post']['smeta'][] = ["url" => $photourl, "alt" => $_POST['photos_alt'][$key]];
                }
            }*/


            $post = I('post.post');
//            $data['smeta'] = json_encode($_POST['post']['smeta']);
            $data['discribe'] = $post['discribe'];
            $data['content'] = htmlspecialchars_decode($post['content']);
            $data['title'] = $post['title'];
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['cover'] = $image[0];
//            var_dump($_POST);
//            var_dump($data);exit;
            if($this->information_model->add($data) == false) {
                $this->error();
            } else {
                $this->success();
            }

        } else {
            $this->assign('options', $options);
            $this->display();
        }
    }


    /**
     * 详情
     */
    public function detail() {
        $id = I('get.id');
        $info = $this->information_model->find($id);
        $info['cover'] = setUrl($info['cover']);

//        $images = json_decode($info['smeta'], true);
        /*foreach(json_decode($info['smeta'], true) as $k => $v) {
            $images[] = setUrl($v['url']);
        }*/
//        var_dump($info['cover']);

        $this->assign('info', $info);
//        $this->assign('images', $images);
        $this->display();
    }

    /**
     * 编辑
     */
    public function edit() {
        $id = I('get.id');
        $info = $this->information_model->where(['id' => $id])->find();
        $types = $this->information_model->types;
        foreach($types as $k => $v) {
            if($v['id'] == $info['inf_type']) {
                $options .= '<option value="'.$v['id'].'" selected>'.$v['name'].'</option>';
            } else {
                $options .= '<option value="'.$v['id'].'" >'.$v['name'].'</option>';
            }

        }
        if(IS_POST) {
            $id = I('post.id');
            $image = upload_img('Information');

            /*if (!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])) {
                foreach ($_POST['photos_url'] as $key => $url) {
                    $photourl = sp_asset_relative_url($url);
                    $_POST['post']['smeta'][] = ["url" => $photourl, "alt" => $_POST['photos_alt'][$key]];
                }
            }*/

            $post = I('post.post');
//            $data['smeta'] = json_encode($_POST['post']['smeta']);
            $data['discribe'] = $post['discribe'];
            $data['content'] = htmlspecialchars_decode($post['content']);
            $data['title'] = $post['title'];
            $data['create_time'] = date('Y-m-d H:i:s');
            if($image) {
                $data['cover'] = $image[0];
            }


            $rst = $this->information_model->where(['id' => $id])->save($data);
            if($rst) {
                $this->success();
            } else {
                $this->error();
            }
        } else {
            $info['cover'] = setUrl($info['cover']);
            $this->assign('info', $info);
//            $this->assign('smeta', json_decode($info['smeta'], true));
            $this->assign('options', $options);
            $this->display();
        }

    }

    /**
     * 删除
     */
    public function delete() {
        $id = I('get.id');
        if($this->information_model->where(['id' => $id])->delete() == false) {
            $this->error();
        } else {
            $this->success();
        }
    }
}